<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *      mark:has readed
 *      $Id: db_driver_mysqli_slave.php 33959 2013-09-06 04:27:59Z nemohou $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
class db_driver_mysqli_slave extends db_driver_mysqli
{

	public $slaveid = null;// $this->serverid.'_'.$sid; 主ID_从ID

	public $slavequery = 0;

	public $slaveexcept = false;//当前表 主服务器读

	public $excepttables = array();

	public $tablename = '';

	protected $_weighttable = array();

	public $serverid = null;

	function set_config($config) {
		parent::set_config($config);
        /**
         *  禁用从数据库的数据表, 表名字之间使用逗号分割
         *
         * @example common_session, common_member 这两个表仅从主服务器读写, 不使用从服务器
         * $_config['db']['common']['slave_except_table'] = 'common_session, common_member';
         *
         */
		if($this->config['common']['slave_except_table']) {
			$this->excepttables = explode(',', str_replace(' ', '', $this->config['common']['slave_except_table']));
		}
	}

	public function table_name($tablename) {
		$this->tablename = $tablename;
		if(!$this->slaveexcept && $this->excepttables) {
			$this->slaveexcept = in_array($tablename, $this->excepttables, true);
		}
		$this->serverid = isset($this->map[$this->tablename]) ? $this->map[$this->tablename] : 1;
		return $this->tablepre.$tablename;
	}

	protected function _slave_connect() {
		if(!empty($this->config[$this->serverid]['slave'])) {
			$this->_choose_slave();//随机选择一个从服务器配置
			if($this->slaveid) {
				if(!isset($this->link[$this->slaveid])) {
					$this->connect($this->slaveid);
				}
				$this->slavequery ++;
				$this->curlink = $this->link[$this->slaveid];
			}
			return true;
		} else {
			return false;//没有启用从服务器的开关
		}
	}
    /**
     * 数据库从服务器设置( slave, 只读 ), 支持多组服务器设置, 当设置多组服务器时, 系统根据每次随机使用
     * @example
     * $_config['db']['1']['slave']['1']['dbhost'] = 'localhost';
     * $_config['db']['1']['slave']['1']['dbuser'] = 'root';
     * $_config['db']['1']['slave']['1']['dbpw'] = 'root';
     * $_config['db']['1']['slave']['1']['dbcharset'] = 'gbk';
     * $_config['db']['1']['slave']['1']['pconnect'] = '0';
     * $_config['db']['1']['slave']['1']['dbname'] = 'x1';
     * $_config['db']['1']['slave']['1']['tablepre'] = 'pre_';
     * $_config['db']['1']['slave']['1']['weight'] = '0'; //权重：数据越大权重越高
     *
     * $_config['db']['1']['slave']['2']['dbhost'] = 'localhost';
     * ...
     *
     */
	protected function _choose_slave(){
		if(!isset($this->_weighttable[$this->serverid])) {
			foreach ($this->config[$this->serverid]['slave'] as $key => $value) //key 1 2 3 4 5
			{
				$this->_weighttable[$this->serverid] .= str_repeat($key, 1 + intval($value['weight']));
			}
		}
		//tablev如1  22 3 444 5555 6
		$sid = $this->_weighttable[$this->serverid][mt_rand(0, strlen($this->_weighttable[$this->serverid]) -1)];
		$this->slaveid = $this->serverid.'_'.$sid;
		if(!isset($this->config[$this->slaveid])) {
			$this->config[$this->slaveid] = $this->config[$this->serverid]['slave'][$sid];
		}
	}

	protected function _master_connect() {
		if ($this->serverid === null) {
			$this->serverid = 1;
		}
		if(!$this->link[$this->serverid]) {
			$this->connect($this->serverid);
		}
		$this->curlink = $this->link[$this->serverid];
	}

	public function query($sql, $silent = false, $unbuffered = false) {
		if(!(!$this->slaveexcept && strtoupper(substr($sql, 0 , 6)) === 'SELECT' && strpos(strtoupper($sql), 'FOR UPDATE') === FALSE && $this->_slave_connect())) //主读写   从服务器只读
		{
			$this->_master_connect();
		}
		$this->tablename = '';
		$this->slaveexcept = false;
		return parent::query($sql, $silent, $unbuffered);
	}

}
?>